//
//  UIImageAdditions.m
//  Layers
//
//  Created by Ben Gotow on 2/15/10.
//  Copyright 2010 Apple Inc. All rights reserved.
//

#import "UIImageAdditions.h"


@implementation UIImage (UIImageAdditions)

- (UIImage*)scaledImage:(float)scale
{
    CGSize s = CGSizeMake([self size].width * scale, [self size].height * scale);
    
    int bitmapBytesPerRow   = (s.width * 4);
    int bitmapByteCount     = (bitmapBytesPerRow * s.height);
    void * bitmapData = calloc( bitmapByteCount, 1);    
    
    
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGRect rect = CGRectMake(0,0,s.width, s.height);
    CGContextRef context = CGBitmapContextCreate (bitmapData, rect.size.width, rect.size.height,8,bitmapBytesPerRow, colorspace, kCGImageAlphaPremultipliedLast);
    CGContextScaleCTM(context, 1, -1);
    CGContextTranslateCTM(context, 0, -s.height);
    
    CGContextDrawImage(context, CGRectMake(0,0, s.width, s.height), [self CGImage]);
    
    
    CGImageRef img = CGBitmapContextCreateImage(context);
    UIImage * result = [UIImage imageWithCGImage: img];
    CGImageRelease(img);
    
    CGColorSpaceRelease(colorspace);
    CGContextRelease(context);
    free(bitmapData);
    
    return result;
}


@end
